package com.gill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gill.entity.MoocBackendUserT;
import com.gill.exception.CommonServiceException;
import com.gill.mapper.MoocBackendUserTMapper;
import com.gill.service.UserService;
import com.gill.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private MoocBackendUserTMapper userMapper;

    @Override
    public String checkUserLogin(String username, String password) throws CommonServiceException {
        // 根据用户名获取用户信息
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_name",username);

        // 避免数据出现问题（可能多个用户名都一样）
        List<MoocBackendUserT> list = userMapper.selectList(queryWrapper);
        MoocBackendUserT user = null;
        if(list !=null && list.size()>0){
            user = list.stream().findFirst().get();//取第一条数据
        }else{
            throw new CommonServiceException(404, "用户名或密码输入有误");
        }

        // 验证密码是否正确【密码要做MD5加密，才能验证是否匹配】
        String encrypt = MD5Util.encrypt(password);

        if(!encrypt.equals(user.getUserPwd())){
            throw new CommonServiceException(500,"用户密码输入有误");
        }else{
            return user.getUuid()+"";
        }
    }
}
